// pages/signUp/index.js
const baseData = require("../../data/college")
const {
  $Toast
} = require('../../iview/base/index');

let path = []
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: String,
    phone: String,
    stuNum: String,
    remarks: String,
    colleges: Array,
    collegeIndex: -1,
    majors: Array,
    majorIndex: -1,
    classes: Array,
    classIndex: -1,
    sex: ['男', '女'],
    sexIndex: -1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let temp = Object.keys(baseData.data)
    this.setData({
      colleges: temp
    })
  },
  collegeChange(e) {
    path = []
    path.push(this.data.colleges[e.detail.value])
    let tempData = baseData.data
    for (let index in path) {
      tempData = tempData[path[index]]
    }
    let tempList = Object.keys(tempData)
    // 要把后一级的index归为-1
    this.setData({
      majors: tempList,
      majorIndex: -1,
      classIndex: -1
    })
  },
  majorChange(e) {
    path = [path[0]]
    path.push(this.data.majors[e.detail.value])
    let tempData = baseData.data
    for (let index in path) {
      tempData = tempData[path[index]]
    }
    // 要把后一级的index归为-1
    this.setData({
      classes: tempData,
      classIndex: -1
    })
  },
  classChange(e) {
    console.log("最终选择了" + path[0] + ":" + path[1] + ":" + this.data.classes[e.detail.value])
  },
  formSubmit(e) {
    let formdata = {
      "name": this.data.name,
      "sex": this.data.sex[this.data.sexIndex],
      "phone": this.data.phone,
      "stuNum": this.data.stuNum,
      "college": this.data.colleges[this.data.collegeIndex],
      "major": this.data.majors[this.data.majorIndex],
      "class": this.data.classes[this.data.classIndex],
      "remarks": this.data.remarks
    }
    if(!this.validate(formdata)){
      return
    }
    wx.showLoading({
      title: '提交中',
    })
    wx.cloud.callFunction({
        // 云函数名称
        name: 'addStudent',
        // 传给云函数的参数
        data: formdata,
      })
      .then(res => {
        wx.showToast({
          title: '提交成功'
      })
      setTimeout(()=>{
        wx.redirectTo({
          url: '../../pages/home/index'
        })
      },1000)
      })
      .catch((err) => {
        wx.showToast({
          icon:'error',
          title: '提交超过10次'
        })
      })

  },
  validate(data){
    // 判空
    if(data.name == String || data.name.length <= 0)
    {
      wx.showToast({
        icon:'error',
        title: '姓名为空'
      })
      return false
    }
    if(data.phone == String || data.phone.length <= 0)
    {
      wx.showToast({
        icon:'error',
        title: '手机号为空'
      })
      return false
    }
    if(data.stuNum == String || data.stuNum.length <= 0)
    {
      wx.showToast({
        icon:'error',
        title: '学号为空'
      })
      return false
    }
    if(data.sex == undefined || data.sex.length <= 0)
    {
      wx.showToast({
        icon:'error',
        title: '性别为空'
      })
      return false
    }
    if(data.college == undefined || data.college.length <= 0)
    {
      wx.showToast({
        icon:'error',
        title: '学院为空'
      })
      return false
    }
    if(data.major == undefined || data.major.length <= 0)
    {
      wx.showToast({
        icon:'error',
        title: '专业为空'
      })
      return false
    }
    if(data.class == undefined || data.class.length <= 0)
    {
      wx.showToast({
        icon:'error',
        title: '班级为空'
      })
      return false
    }

    // 格式校验
    if(data.phone.length!=11)
    {
      wx.showToast({
        icon:'error',
        title: '手机号格式错误'
      })
      return false
    }
    
    return true
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})